﻿Imports TBCC.UtilityMobile
Public Class TimeSetForm

    '起始地址
    Private _offset As UShort = 0

    '时间参数数据
    Private _buf() As Short

    Private _pi As ProjectItem

    Private Sub TimeSetForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        _pi = Me.MyParentForm.PI

        EnableModify(False)
        Me.btnModify.Enabled = False
        Me.btnShowDevTime.Enabled = False


        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(False)
        End If
        Timer1.Start() ' LoadData()
    End Sub


    Private Sub LoadData()
        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = COMHelper.Instance.Send4Short(g_netID, FunctionCode.WordReadonly, _offset, 7) '读1号子控时间
            If _buf Is Nothing Then
                msg.ShowError(g_strReadError)
                Me.nudYear.Minimum = 0
                Me.nudYear.Value = 0
                Me.btnModify.Enabled = False
                Me.btnShowDevTime.Enabled = False
                Exit Sub
            End If
        End If

        Me.btnModify.Enabled = True
        Me.btnShowDevTime.Enabled = True

        Try
            Me.nudYear.Value = _buf(0) + 1900
            Me.nudMonth.Value = _buf(1)
            Me.nudDay.Value = _buf(2)
            Me.nudHour.Value = _buf(3)
            Me.nudMinute.Value = _buf(4)
            Me.nudSecond.Value = _buf(5)
        Catch ex As Exception
            msg.ShowError(ex.Message)
        End Try


    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub



    Private Sub EnableModify(ByVal value As Boolean)
        Me.btnModify.Enabled = Not value
        Me.btnOk.Enabled = value
        Me.btnShowDevTime.Enabled = Not value

        Me.nudYear.ReadOnly = Not value
        Me.nudMonth.ReadOnly = Not value
        Me.nudDay.ReadOnly = Not value
        Me.nudHour.ReadOnly = Not value
        Me.nudMinute.ReadOnly = Not value
        Me.nudSecond.ReadOnly = Not value

        Dim i As Integer
        If value Then
            i = 1
        Else
            i = 0
        End If

        Me.nudYear.Increment = i
        Me.nudMonth.Increment = i
        Me.nudDay.Increment = i
        Me.nudHour.Increment = i
        Me.nudMinute.Increment = i
        Me.nudSecond.Increment = i

    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        If _pi.LocalRunStatus = LocalRunStatus.Start Then
            msg.ShowError("终端运行未停止，无法修改时间。")
            Exit Sub
        End If
        EnableModify(True)
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim result As Boolean
        Me.Cursor = Cursors.WaitCursor
        Me.TableLayoutPanel1.Enabled = False
        result = Save()
        Me.TableLayoutPanel1.Enabled = True
        Me.Cursor = Cursors.Default

        If result Then
            msg.Show("保存成功。")
        Else
            Exit Sub
        End If

        EnableModify(False)
    End Sub

    ''' <summary>
    ''' 保存数据
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean
        Dim result As Boolean

        Select Case _pi.ProjectType
            Case ProjectType_Enum.Car '移动车载
                result = Write(1)
            Case ProjectType_Enum.Box '小批零
                result = Write(1)
                'result = Write(2) And result '暂时不支持2号设备
            Case Else
                Exit Function
        End Select

        If result = False Then
            msg.ShowError("修改时间失败。")
        End If

        Return result
    End Function

    Private Function ReadAllDevTime() As String

        ReadAllDevTime = String.Empty
        Dim str As String = Nothing
        Dim netcount As Integer

        Select Case _pi.ProjectType
            Case ProjectType_Enum.Car
                netcount = 1
            Case ProjectType_Enum.Box
                netcount = 1 '暂时不支持2号设备
        End Select

        Try
            For netid As Integer = 1 To netcount
                str = _ReadDevTime(netid)
                If str IsNot Nothing Then
                    ReadAllDevTime &= str & vbCrLf
                Else
                    ReadAllDevTime &= netid & "号设备系统时间：" & g_strReadError & vbCrLf
                End If
            Next
        Catch ex As Exception
            ReadAllDevTime = String.Empty
            msg.Show(ex.Message)
        End Try

    End Function
    Private Function _ReadDevTime(ByVal netID As Integer) As String
        _ReadDevTime = String.Empty

        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = __ReadDevTime(netID)
            If _buf Is Nothing Then
                Me.nudYear.Minimum = 0
                Me.nudYear.Value = 0
                'Me.btnModify.Enabled = False
                Return Nothing
            End If
        End If

        If netID = 255 Then
            _ReadDevTime &= " 主设备系统时间："
        Else
            _ReadDevTime &= netID & "号设备系统时间："
        End If

        If netID = g_netID Then
            Me.nudYear.Value = _buf(0) + 1900
            Me.nudMonth.Value = _buf(1)
            Me.nudDay.Value = _buf(2)
            Me.nudHour.Value = _buf(3)
            Me.nudMinute.Value = _buf(4)
            Me.nudSecond.Value = _buf(5)
        End If

        _ReadDevTime &= String.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", _
            _buf(0) + 1900, _buf(1), _buf(2), _buf(3), _buf(4), _buf(5))

    End Function
    Private Function __ReadDevTime(ByVal netID As Integer) As Short()

        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = COMHelper.Instance.Send4Short(netID, FunctionCode.WordReadonly, _offset, 7) '读1号子控时间
        End If

        Return _buf
    End Function

    Private Function Write(ByVal netid As Integer) As Boolean
        Dim value As Byte

        Dim result As Boolean = False

        value = Me.nudYear.Value - 1900
        If Not Write(netid, 0, value) Then
            GoTo WriteERROR
        End If
        Sleep()

        value = Me.nudMonth.Value
        If Not Write(netid, 1, value) Then
            GoTo WriteERROR
        End If
        Sleep()

        value = Me.nudDay.Value
        If Not Write(netid, 2, value) Then
            GoTo WriteERROR
        End If
        Sleep()

        value = Me.nudHour.Value
        If Not Write(netid, 3, value) Then
            GoTo WriteERROR
        End If
        Sleep()

        value = Me.nudMinute.Value
        If Not Write(netid, 4, value) Then
            GoTo WriteERROR
        End If
        Sleep()

        value = Me.nudSecond.Value
        If Not Write(netid, 5, value) Then
            GoTo WriteERROR
        End If
        Sleep()

        'value = Me.nudWeek.Value
        'COMHelper.Instance.Broadcast(functionCode, 6, value)
        'sleep()

        '修改使能位
        value = 1
        If Not Write(netid, 7, value) Then
            GoTo WriteERROR
        End If

        GoTo WriteSUCCESS

WriteERROR:
        Return False

WriteSUCCESS:
        Return True

    End Function

    Private Function Write(ByVal netid As Byte, ByVal offset As UShort, ByVal value As Byte) As Boolean
        Return COMHelper.Instance.Write(netid, FunctionCode.WordWrite, offset, value) '非广播地址
    End Function

    Sub Sleep()
        System.Threading.Thread.Sleep(100)
    End Sub

    Private Sub nudYear_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudYear.ValueChanged

    End Sub

    Private Sub nudMonth_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudMonth.ValueChanged

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        LoadData()
        If Me.MyParentForm IsNot Nothing Then
            Me.MyParentForm.SetControlEnabled(True)
        End If
    End Sub

    Private Sub btnShowDevTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowDevTime.Click
        sender.Enabled = False
        Me.btnModify.Enabled = False
        Dim str As String
        str = ReadAllDevTime()
        If str.Length > 0 Then
            msg.Show(str)
        End If
        sender.Enabled = True
        Me.btnModify.Enabled = True
    End Sub
End Class